package xiangmu.schoolpaper.controller.bingo.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import xiangmu.schoolpaper.pojo.DTO.SystemTemplateOneByIdDTO;
import xiangmu.schoolpaper.pojo.DTO.SystemTemplateParameterByIdDTO;
import xiangmu.schoolpaper.pojo.DTO.SystemTemplateStatusDTO;
import xiangmu.schoolpaper.pojo.DTO.SystemTemplateWeightUpdateDTO;
import xiangmu.schoolpaper.pojo.VO.*;
import xiangmu.schoolpaper.result.Result;
import xiangmu.schoolpaper.service.system.PredictionKeywordsService;
import xiangmu.schoolpaper.service.system.SystemTemplateManagementService;
import xiangmu.schoolpaper.service.teacher.TemplateTypesService;

import java.util.List;

@RestController
@RequestMapping("/system/template")
@Slf4j
@Tag(name = "管理员端——4数据管理——1模板管理")
public class SystemTemplateManagementController {

    @Autowired
    private PredictionKeywordsService predictionKeywordsService;

    @Autowired
    private TemplateTypesService templateTypesService;

    @Autowired
    private SystemTemplateManagementService systemTemplateManagementService;

    @Operation(summary = "1获取所有专业")
    @GetMapping("/get-all-majors")
    public Result<List<String>> getAllMajorsST(){
        log.info("获取所有专业名称");
        List<String> list = predictionKeywordsService.getAllMajors();
        return Result.success(list);
    }

    @Operation(summary = "2获取所有模板类型")
    @GetMapping("/get-all-template-types")
    public Result<List<String>> getAllTemplateTypesST(){
        log.info("获取所有模版类型名称");
        List<String> list = templateTypesService.getAllTemplateTypes();
        return Result.success(list);
    }

    @Operation(summary = "3分页查询所有符合筛选条件的模板记录")
    @GetMapping("/get-all-templates-by-pages")
    public Result<IPage<SystemTemplatesInfoVO>> getAllTemplatesByPages(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer facultyId,
            @RequestParam(required = false) String major,
            @RequestParam(required = false) String type
    ) {
        log.info("分页查询所有符合筛选条件的模板记录");
        IPage<SystemTemplatesInfoVO> pageData = systemTemplateManagementService.getAllTemplatesByPages(currentPage, pageSize, facultyId, major, type);
        return Result.success(pageData);
    }

    @Operation(summary = "4修改模板启用状态")
    @PostMapping("/update-template-status-by-id")
    public Result updateTemplateStatusById(@RequestBody SystemTemplateStatusDTO systemTemplateStatusDTO) {
//        规则 1：同一种模板（major_id + type_id 相同）始终必须有且只有一个 status = 1
//        规则 2：如果前端想把唯一启用的模板改为 0，必须拦截，返回提示 "每种模板必须有且只有一个为启用状态"
//        规则 3：如果前端传回来的目标是 status = 1，就执行“把它设为 1，其余设为 0”
        log.info("修改模板启用状态");
        Integer result = systemTemplateManagementService.updateTemplateStatusById(systemTemplateStatusDTO);
        if(result == -2) {
            return Result.error("当前模板不存在");
        } else if(result == -1) {
            return Result.error("每种模板必须有且只有一个为启用状态");
        } else if(result == 1) {
            return Result.success("模板状态修改成功");
        } else {
            return Result.success("当前模板状态无需修改");
        }
    }

    @Operation(summary = "5根据ID查看某一条模板基本信息")
    @GetMapping("/get-template-one-by-id")
    public Result<SystemTemplateOneByIdVO> getTemplateOneById(@RequestParam Integer templateId) {
        log.info("根据ID查看某一条模板基本信息");
        SystemTemplateOneByIdVO one = systemTemplateManagementService.getTemplateOneById(templateId);
        return Result.success(one);
    }

//    【已废弃******后续升级修改逻辑可做微调后使用】
//    @Operation(summary = "6修改某一条模板基本信息")
//    @PostMapping("/update-template-one-by-id")
//    public Result updateTemplateOneById(@RequestBody SystemTemplateOneByIdDTO systemTemplateOneByIdDTO) {
//        try {
//            Boolean success = systemTemplateManagementService.updateTemplateOneById(systemTemplateOneByIdDTO);
//            if(success) {
//                return Result.success("该模板基本信息修改成功");
//            } else {
//                return Result.error("该模版基本信息修改失败");
//            }
//        } catch (RuntimeException e) {
//            return Result.error(e.getMessage());
//        }
//    }

    @Operation(summary = "6修改某一条模板的模板名称")
    @PostMapping("/update-template-name-by-id")
    public Result updateTemplateNameById(@RequestBody SystemTemplateOneByIdDTO systemTemplateOneByIdDTO) {
        log.info("修改某一条模板的模板名称");
        try {
            Boolean success = systemTemplateManagementService.updateTemplateNameById(systemTemplateOneByIdDTO);
            if(success) {
                return Result.success("该模板名称修改成功");
            } else {
                return Result.error("该模版名称修改失败");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "7根据ID查看某一条模板评分参数")
    @GetMapping("/get-template-parameter-by-id")
    public Result<SystemTemplateParameterByIdVO> getTemplateParameterById(@RequestParam Integer templateId) {
        log.info("根据ID查看某一条模板评分参数");
        SystemTemplateParameterByIdVO params = systemTemplateManagementService.getTemplateParameterById(templateId);
        return Result.success(params);
    }

    @Operation(summary = "8根据ID修改某一条模板评分参数")
    @PostMapping("/update-template-parameter-by-id")
    public Result updateTemplateParameterById(@RequestBody SystemTemplateParameterByIdDTO systemTemplateParameterByIdDTO) {
        log.info("根据ID修改某一条模板评分参数");
        try {
            Boolean success = systemTemplateManagementService.updateTemplateParameterById(systemTemplateParameterByIdDTO);
            if(success) {
                return Result.success("该模板参数修改成功");
            } else {
                return Result.error("该模版参数修改失败");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
    @Operation(summary = "9根据ID查看某一条模板章节列表")
    @GetMapping("/get-template-chapter-by-id")
    public Result<SystemTemplateChapterByIdVO> getTemplateChapterById(@RequestParam Integer templateId) {
        log.info("根据ID查看某一条模板章节列表");
        try {
            SystemTemplateChapterByIdVO params = systemTemplateManagementService.getTemplateChapterById(templateId);
            return Result.success(params);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "10根据ID查看某一条模板权重信息")
    @GetMapping("/get-template-weight-one-by-id")
    public Result<SystemTemplateWeightByIdVO> getTemplateWeightOneById(Integer templateId) {
        log.info("根据ID查看某一条模板权重信息");
        SystemTemplateWeightByIdVO data = systemTemplateManagementService.getTemplateWeightOneById(templateId);
        if (data == null || CollectionUtils.isEmpty(data.getWeights())) {
            return Result.error("未查询到该模板的权重信息");
        }
        return Result.success(data);
    }
    @Operation(summary = "11修改某一条模板权重信息")
    @PostMapping("/update-template-weight-one-by-id")
    public Result updateTemplateWeightOneById(@RequestBody SystemTemplateWeightUpdateDTO dto) {
        try {
            Boolean success = systemTemplateManagementService.updateTemplateWeightOneById(dto);
            return Result.success(success);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
//    @Operation(summary = "12导入模板")
//    @Operation(summary = "13下载模板")
}
